package com.zzyl.nursing.service.impl;

import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IMemberReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @author Waker
 * @description:
 * @create 2025-09-13-21:56
 */
@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements IMemberReservationService {
    @Autowired
    private IFamilyMemberService familyMemberService;

    /**
     * 查询当天取消预约数量
     *
     * @return
     */
    @Override
    public Integer getCancelledReservationCount() {
        // 查询reservation表 获取当天取消预约数量
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime startTime = now.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        Long count = lambdaQuery()
                .between(Reservation::getUpdateTime, startTime, endTime)
                .eq(Reservation::getStatus, 2)
                .count();
        // 返回数量
        return Math.toIntExact(count);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<Map<String, Object>> countByTime(Long time) {
        LocalDateTime ldt = LocalDateTimeUtil.of(time);
        LocalDateTime startTime = ldt.toLocalDate().atStartOfDay();
        LocalDateTime endTime = startTime.plusDays(1);
        return getBaseMapper().countByTime(startTime, endTime);
    }

    /**
     * 新增预约
     *
     * @param reservation
     */
    @Override
    public void saveReservation(Reservation reservation) {
        reservation.setStatus(0);
        save(reservation);
    }

    /**
     * 查询预约列表
     *
     * @param reservation
     * @return
     */
    @Override
    public List<Reservation> seleltReservationList(Reservation reservation) {
//        // 1.从ThreadLocal中获取用户id
        Long userId = UserThreadLocal.getUserId();
//        // 2.查询family_member表 获取用户手机号
        FamilyMember familyMember = familyMemberService.getById(userId);
        String phone = familyMember.getPhone();
        return lambdaQuery()
                .eq(ObjectUtil.isNotNull(reservation.getStatus()), Reservation::getStatus, reservation.getStatus())
                .eq(Reservation::getMobile, phone)
                .list();
    }

    /**
     * 取消预约
     *
     * @param id
     */
    @Override
    public void cancelReservation(Long id) {
        lambdaUpdate()
                .set(Reservation::getStatus, 2)
                .eq(Reservation::getId, id)
                .update();
    }

    /**
     * 更新预约状态
     */
    @Override
    public void updateReservationStatus() {
        // 1.查询所有状态为0（待报道）的预约 预约时间小于当前时间 设置状态为3（过期）
        lambdaUpdate()
                .set(Reservation::getStatus, 3)
                .set(Reservation::getUpdateTime, LocalDateTime.now())
                .eq(Reservation::getStatus, 0)
                .lt(Reservation::getTime, LocalDateTime.now())
                .update();
    }
}
